template.defaults.imports.CountdownTime = function (factEndDate) {
    var timestamp = (new Date()).valueOf();
    return factEndDate - timestamp;
};
    /* global orderStates */

    var loading = false; //状态标记
    var hasNextPage = false;
    var pageNo = 1;
    var orderState = orderStates;
    queryList(pageNo);

    //下拉刷新
    $("#aroundBox").pullToRefresh().on("pull-to-refresh", function () {
        $("#contentTo").html("");
        pageNo = 1;
        queryList(pageNo,function(){
            $("#aroundBox").pullToRefreshDone(); // 重置下拉刷新
        });
    });

    //上拉加载更多
    $('#aroundBox').infinite().on("infinite", function () {
        if (hasNextPage){
            pageNo++;
            queryList(pageNo,function(){
            },true);
        }        
    });

    function queryList(pageNo,callback,isMore){
        if (loading) return;
        loading = true;
        var params = {};
        params.start = (pageNo - 1) * 20;
        params.limit = 20;
        /* global getCookie */
        params.token = getCookie('token');
        params.state = orderState;
        if (orderState == -1) {
            delete params.state;
        } 


        $.post('/antique/api/mallOrder/shop/query_order_list', params, function (result) {
            if (!isMore){
                /* global template visitUrl*/
                console.log(result.data);
                document.getElementById('contentTo').innerHTML = template('content', result.data);                
            } else {
                $("#contentTo").append(template('content', result.data));
            }
            hasNextPage = result.data.hasNextPage;
            if (!result.data.hasNextPage) {
                $('#loadmore').hide();
                $('#nomore').show();
            } else {
                $('#loadmore').show();
                $('#nomore').hide();
            }
            if (callback) callback();
            loading = false;
        });
    }

    $(".weui-tab .tabs .item").click(function () {
        $(".weui-tab .tabs .item").removeClass("current");
        $(this).addClass("current");
    
        orderState = $(this).data('state');
        $("#contentTo").html("");
        pageNo = 1;
        queryList(pageNo,function(){
            $("#aroundBox").pullToRefreshDone(); // 重置下拉刷新
        });
        
    });

    function getlogisticscompany(logisticsValue){
    $.ajax({
        type: 'POST',
        url: "/antique/api/sysDict/query_list",
        data: {
            type: "logisticscompany",
            "token": getCookie('token')
        },
        dataType: "json",
        success: function (result) {
            if (result.code == 0) {
                if (result.data && result.data.list && result.data.list.length > 0) {
                    var list = result.data.list;
                    for (var i = 0; i < list.length; i++) {
                        if ( logisticsValue == list[i].value){
                            $("#logisticsCompany").append("<option select value='" + list[i].value + "'>" + list[i].label + "</option>");
                        } else {
                            $("#logisticsCompany").append("<option value='" + list[i].value + "'>" + list[i].label + "</option>");
                        }
                        
                    }
                }
            }
        }
    });
    }
    function logistics (orderId,logisticsCode ,sendLogisticsNo) { 
        getlogisticscompany(logisticsCode );
        $.modal({
          title: "填写物流信息",
          text: '<input type="hidden" id="orderId" value="' + orderId + '" />' +
          '<select class="weui-select selectss" id="logisticsCompany"><option disabled selected style=display:none;">请选择物流公司</option></select>' + 
          '<input class="inputss" type="text" id="sendLogisticsNo" value="' + sendLogisticsNo + '" placeholder="请输入快递单号" />',
          buttons: [ 
            { text: "确定", onClick: function(){
               
                var receiveLogisticsCode = $("#logisticsCompany").val();
                
                var receiveLogisticsNo = $("#sendLogisticsNo").val();
                var id = $("#orderId").val();
                
                 if (receiveLogisticsCode == ''){
                     $.toast("公司不能为空", "cancel");
                     return;
                 }
                 if (receiveLogisticsNo == ''){
                     $.toast("单号不能为空", "cancel");
                     return;
                 }
                 var receiveLogisticsCompany = $("#logisticsCompany").find("option:selected").text();
                 var params = {};
                 params.id = id;
                 params.token = getCookie('token');
                 params.receiveLogisticsCompany = receiveLogisticsCompany;
                 params.receiveLogisticsCode = receiveLogisticsCode;
                 params.receiveLogisticsNo = receiveLogisticsNo;
            
                $.post('/antique/api/mallOrder/shop/order_update_detail', params, function (result) {
                    if (result.code == 0) {
                        sureDeliver (id);
                    } else {
                        $.toast(result.message, "cancel");
                    }
                    });
             } },
            { text: "取消", className: "default" }
          ]
        });
      }

    function sureDeliver (orderId) {
        $.post('/antique/api/mallOrder/shop/order_send_goods', { id:orderId,token:getCookie('token') }, function (result) {
            if (result.code == 0) {
                $("#contentTo").html("");
                pageNo = 1;
                queryList(pageNo,function(){},true);
                $.toast("操作成功！");
            } else {
                $.toast(result.message, "cancel");
            }
        });   
        
    }
    function opRefund (orderId,type) {
        $.confirm("您是否确认该操作？", "", function() {
            $.post('/antique/api/mallOrder/shop/order_refund_audit', { id:orderId,token:getCookie('token'),oper:type }, function (result) {
                if (result.code == 0) {
                    $.toast("操作成功！");
                    $("#contentTo").html("");
                    pageNo = 1;
                    queryList(pageNo,function(){},true);
                } else {
                 $.toast(result.message, "cancel");
                }
             });
          }, function() {
            //取消操作
          });
        
    }
    function finishRefund (orderId) {
        $.confirm("您是否确认完成？", "", function() {
            $.post('/antique/api/mallOrder/shop/order_refund_complate', { id:orderId,token:getCookie('token') }, function (result) {
                if (result.code == 0) {
                    $.toast("操作成功！");
                    $("#contentTo").html("");
                    pageNo = 1;
                    queryList(pageNo,function(){},true);
                } else {
                 $.toast(result.message, "cancel");
                }
             });
          }, function() {
            //取消操作
          });
        
    }
    function upPrice (orderId) {
        $.modal({
            title: "修改价格",
            text: '<input class="weui-input weui-prompt-input"  type="number" id="money"  placeholder="输入想要修改的价格" />',
            buttons: [ 
                { text: "取消", className: "default" },
                { text: "确定", onClick: function(){
                   var money = $("#money").val();
                  
                    var reg = new RegExp("^[0-9]+([.]{1}[0-9]+){0,1}$"); 
                    if (!reg.test(money)){
                        $.toast("价格格式错误", "cancel");
                        return;
                    }
                    $.post('/antique/api/mallOrder/shop/order_modify_money', { id:orderId,token:getCookie('token') ,money : money }, function (result) {
                        if (result.code == 0) {
                            $.toast("修改成功！");
                            $("#contentTo").html("");
                            pageNo = 1;
                            queryList(pageNo,function(){},true);
                        } else {
                         $.toast(result.message, "cancel");
                        }
                     });
                } }
                
            ]
        });
           
    }